import AntModal from "@/components/pt/dialog/AntModal";
import FileUpload from "@/components/pt/fileupload/IndexList";
import {
  getBizTaizhang005,
  addBizTaizhang005,
  updateBizTaizhang005,
  findMaxSort,
} from "@/api/biz/bizTaizhang005";

export default {
  name: "CreateForm",
  props: {},
  components: {
    AntModal,
    FileUpload,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      formTitle: "添加制度建设台账",
      fileList: [],
      // 表单参数
      form: {},
      rules: {
        tz00501: [{ required: true, message: "请输入文号", trigger: "blur" }],
        tz00502: [
          { required: true, message: "请选择印发时间", trigger: "change" },
        ],
        tz00503: [
          { required: true, message: "请输入制度名称", trigger: "blur" },
        ],
      },
    };
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    onClose() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,

        deptId: undefined,

        userName: undefined,

        deptName: undefined,

        companyId: undefined,

        tz00501: undefined,

        tz00502: undefined,

        tz00503: undefined,

        fileUrl: undefined,

        fileName: undefined,

        sort: undefined,

        status: undefined,
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
        this.formTitle = "添加制度建设台账";
      });
    },

    uploadCompleteFile(item, items) {
      this.fileList = [];
      console.log(("item", item, items));
      this.$message.success("上传成功", 3);
      this.fileList = items;
      this.form.fileUrl = item.url;
      this.form.fileName = item.fileName;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizTaizhang005Id = row.id;
      getBizTaizhang005(bizTaizhang005Id).then((response) => {
        this.form = response.data;
        this.formTitle = "修改制度建设台账";
        this.spinning = !this.spinning;
        this.fileList[0] = {
          url: this.form.fileUrl,
          name: this.form.fileName,
          status: "done",
          uid: this.form.id,
        };
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form));
          if (this.form.id !== undefined) {
            updateBizTaizhang005(saveForm).then((response) => {
              this.$message.success("修改成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          } else {
            addBizTaizhang005(saveForm).then((response) => {
              this.$message.success("新增成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          }
        } else {
          return false;
        }
      });
    },
    back() {
      const index = "/biz/taizhang005/index";
      this.$router.push(index);
    },
  },
};
